package com.beta.listener;

import com.alibaba.fastjson.JSON;
import com.beta.entity.OrderItems;
import com.beta.entity.ProductItems;
import com.beta.event.order.OrderItemPayloadEvent;
import com.beta.service.ICleanTxnDetailsService;
import com.beta.service.IProductItemsService;
import com.beta.service.IPushToAppService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * Created by vector on 2016/12/7.
 */
@Component
public class OrderItemEventListener {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IPushToAppService pushToAppService;
    @Autowired
    private ICleanTxnDetailsService cleanTxnDetailsService;
    @Autowired
    private IProductItemsService productItemsService;

    @EventListener(condition = "T(com.beta.event.order.OrderItemEvent).REFUND.equals(#payloadEvent.event)")
    public void onRefund(OrderItemPayloadEvent payloadEvent) {
        OrderItems orderItems = payloadEvent.getPayload();
        logger.info("订单项退款 ==> orderItem - {}", JSON.toJSONString(orderItems));

        ProductItems productItems = productItemsService.selectById(orderItems.getProductItemId());
        Integer ownerId = productItems.getUserId();
        if (Objects.nonNull(ownerId)) {
            pushToAppService.pushToSingle(ownerId, "您有一笔退货单要处理", "rejected");
        }

        cleanTxnDetailsService.recordOrderItemRefund(orderItems);
    }

    @EventListener(condition = "T(com.beta.event.order.OrderItemEvent).REFUND_CANCEL.equals(#payloadEvent.event)")
    public void onRefundCancel(OrderItemPayloadEvent payloadEvent) {
        OrderItems orderItems = payloadEvent.getPayload();
        logger.info("订单项取消退款 ==> orderItem - {}", JSON.toJSONString(orderItems));

        ProductItems productItems = productItemsService.selectById(orderItems.getProductItemId());
        Integer ownerId = productItems.getUserId();
        if (Objects.nonNull(ownerId)) {
            pushToAppService.pushToSingle(ownerId, "您的退货单已取消", "unrejected");
        }
    }

    @EventListener(condition = "T(com.beta.event.order.OrderItemEvent).REFUND_AGREE.equals(#payloadEvent.event)")
    public void onRefundAgree(OrderItemPayloadEvent payloadEvent) {
        OrderItems orderItems = payloadEvent.getPayload();
        logger.info("订单项同意退款 ==> orderItem - {}", JSON.toJSONString(orderItems));

        cleanTxnDetailsService.recordOrderItemRefundAgree(orderItems);
    }

    @EventListener(condition = "T(com.beta.event.order.OrderItemEvent).REFUND_SETTLED.equals(#payloadEvent.event)")
    public void onRefundSettled(OrderItemPayloadEvent payloadEvent) {
        OrderItems orderItems = payloadEvent.getPayload();
        logger.info("订单项退款结算 ==> orderItem - {}", JSON.toJSONString(orderItems));

        cleanTxnDetailsService.recordOrderItemRefundSettled(orderItems);
    }

}